<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class PermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        app()['cache']->forget('spatie.permission.cache');
        // 添加权限
        $permissions = [
            // 权限名字需要唯一，刚好路由名字也是唯一，采用路由名字
            ['name' => 'users.index', 'guard_name' => 'admin'],
            ['name' => 'users.detail', 'guard_name' => 'admin'],
            ['name' => 'users.delete', 'guard_name' => 'admin'],
        ];
        // 添加权限
        foreach($permissions as $p) {
            Permission::create($p);
        }

        // 添加角色
        $role = Role::create(['name' => 'superAdmin', 'guard_name' => 'admin']);

        // 为角色添加权限 所有权限给到超级管理员
        $role->givePermissionTo(Permission::all()); //revokePermissionTo

        /* 删除权限的2种方法
         *  $role->revokePermissionTo($permission);
            $permission->removeRole($role);
         * */

    }
}
